package no.ntnu.fp.model;

import java.sql.Date;
import java.sql.Time;
import java.util.Calendar;

public class CalendarHelper {
	private String monthString;
	private String dayString;
	
	private Calendar cal;
	private int day;
	private int week;
	private int month;
	private int year;
	private int dow;
	private int dom;
	private int doy;
	private int monthsize;
	
	
	//private int[] array1 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	
	public CalendarHelper(){
		cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		day = cal.get(Calendar.DATE);
		week = cal.get(Calendar.WEEK_OF_YEAR);
		month = cal.get(Calendar.MONTH) + 1;
		year = cal.get(Calendar.YEAR);
		dow = cal.get(Calendar.DAY_OF_WEEK);
		dom = cal.get(Calendar.DAY_OF_MONTH);
		doy = cal.get(Calendar.DAY_OF_YEAR);
		monthsize = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		
	}
	public void updateWhenJump(){
		day = cal.get(Calendar.DATE);
		week = cal.get(Calendar.WEEK_OF_YEAR);
		month = cal.get(Calendar.MONTH) + 1;
		year = cal.get(Calendar.YEAR);
		dow = cal.get(Calendar.DAY_OF_WEEK);
		dom = cal.get(Calendar.DAY_OF_MONTH);
		doy = cal.get(Calendar.DAY_OF_YEAR);
		monthsize = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	public void jumpWeek(){
		cal.add(Calendar.WEEK_OF_YEAR, 1);
		updateWhenJump();
	}
	
	public void jumpBackWeek(){
		cal.add(Calendar.WEEK_OF_YEAR, -1);
		updateWhenJump();
	}
	
	public void jumpDay(){
		cal.add(Calendar.DAY_OF_YEAR, 1);
		updateWhenJump();
	}
	
	public void jumpBackDay(){
		cal.add(Calendar.DAY_OF_YEAR, -1);
		updateWhenJump();
	}
	
	public String getDateString(){
		String date = day+"."+month+"."+year;
		return date;
	}
	
	public Date getDateNow(){
		return new Date(Calendar.getInstance().getTimeInMillis());
	}
	
	public Time getDateTime(){
		return new Time(Calendar.getInstance().getTimeInMillis());
	}
	
	public Date getFirstCalendarDayDate(){
		return new Date(cal.getTimeInMillis());
	}
	
	public Time getDateFirstCalendarDayTime(){
		return new Time(cal.getTimeInMillis());
	}
	
	public int getDayDate(int d){
		int gdd = dom+d;
		
		if(gdd > monthsize){
			return gdd-monthsize;		
		}
		else{
			return gdd;
		}
	}
	
	public int[] getWholeWeekDay(){
		int daysarray[] = new int[7];
		
		for (int i = 0; i < daysarray.length; i++) {
			daysarray[i] = getDayDate(i);
		}
		return daysarray;
	}
	
	public String[] getWholeWeek(){
		String[] array = new String[7];
		
		for (int i = 0; i < array.length; i++) {
			array[i] = getDayOfWeek(i-1);
		}
		return array;
	}
	
	public int getWeek(){
		return week;
	}
	
	public int getYear(){
		return year;
	}

	
	public String getMonth(int nr){
		int m = month +nr;
		if(month+nr >12&& month+nr<=24){
			m = month+nr-12;
		}
		else if(month+nr <1 && month+nr>=-12){
			m= month+nr+12;
		}
		switch(m){
		case 1:  monthString = "Januar"; 	break;
		case 2:  monthString = "Februar";	break;
		case 3:  monthString = "Mars";		break;
		case 4:  monthString = "April";		break;
		case 5:  monthString = "Mai";		break;
		case 6:  monthString = "Juni";		break;
		case 7:  monthString = "Juli";		break;
		case 8:  monthString = "August"; 	break;
		case 9:  monthString = "September";	break;
		case 10: monthString = "Oktober";	break;
		case 11: monthString = "November";	break;
		case 12: monthString = "Desember";	break;
		default: monthString = "Invalid";	break;
		}
		return monthString;
	}
	public int getD(){
		return dow;
	}

	
	public String getDayOfWeek(int nr){
		int d= dow+nr;
		if(d > 7&& d <= 14){
			d= dow+nr-7;
		}
		else if(dow+nr <1 && dow+nr >-7){
			d=dow+nr+7;
		}
		switch(d){
		case 1: dayString = "Mandag";	break;
		case 2: dayString = "Tirsdag";	break;
		case 3: dayString = "Onsdag";	break;
		case 4: dayString = "Torsdag";	break;
		case 5: dayString = "Fredag";	break;
		case 6: dayString = "L\u00F8rdag";	break;
		case 7: dayString = "S\u00F8ndag";	break;
		default: dayString = "lol";	break;
		}
		return dayString;
		
	}
	
	public Date getFirstDayInCurrentWeek(){
		
		Calendar calTemp =  (Calendar) cal.clone();
		calTemp.set(Calendar.MINUTE, 0);
		calTemp.set(Calendar.HOUR_OF_DAY, 0);
		return new java.sql.Date(calTemp.getTimeInMillis());
		
	}
	
	
	/**
	 * @return
	 */
	public Date getLastDayInCurrentWeek(){
		
		Calendar calTemp = (Calendar) cal.clone();
		
		calTemp.add(Calendar.DAY_OF_YEAR, 6);
		calTemp.set(Calendar.MINUTE, 59);
		calTemp.set(Calendar.HOUR_OF_DAY, 23);
		return new Date(calTemp.getTimeInMillis());
		
	}
	
	public int getDayOfWeekNow(){
		
		return (Calendar.getInstance().DAY_OF_WEEK+6)%7;
		
	}
	
	public int getCurrentHour(){
		return cal.getTime().getHours();
	}
	
	public int getCurrentMinute(){
		return cal.getTime().getMinutes();
	}
	
	

}
